
package com.warmlight.voicepacket.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class VoicePacketProvider extends ContentProvider {

    public static final String AUTHORITY = "com.warmlight.voicepacket.provider";
    public static final Uri URI_TABLE_LIST = Uri.parse("content://" + AUTHORITY + "/"
            + VoicePacketDBHelper.TABLE_LIST);
    public static final Uri URI_TABLE_AUDIO = Uri.parse("content://" + AUTHORITY + "/"
            + VoicePacketDBHelper.TABLE_AUDIO);
    private static final int OPERATION = 1;

    private static UriMatcher sUriMatcher = null;
    private VoicePacketDBHelper mHelper;
    private SQLiteDatabase mDatabase;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(VoicePacketProvider.AUTHORITY,
                VoicePacketDBHelper.TABLE_LIST, OPERATION);
        sUriMatcher.addURI(VoicePacketProvider.AUTHORITY,
                VoicePacketDBHelper.TABLE_AUDIO, OPERATION);
    }

    @Override
    public boolean onCreate() {
        mHelper = new VoicePacketDBHelper(getContext());
        mHelper.getReadableDatabase();
        return false;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        if (sUriMatcher.match(uri) != OPERATION) {
            return 0;
        }
        mDatabase = mHelper.getWritableDatabase();
        count = mDatabase.delete(getDATABASE_TABLE(uri),
                selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (sUriMatcher.match(uri) != OPERATION) {
        	Log.w("Provider", "insert operation uri does not match");
            return null;
        }
        mDatabase = mHelper.getWritableDatabase();
        long _id = mDatabase.insert(getDATABASE_TABLE(uri),
                null, values);
        if (_id > 0) {
            Uri profileUri = ContentUris.withAppendedId(uri, _id);
            getContext().getContentResolver().notifyChange(uri, null);
            return profileUri;
        } else {
            throw new SQLException("Failed to insert row into " + uri);
        }
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        if (sUriMatcher.match(uri) != OPERATION) {
            return null;
        }
        mDatabase = mHelper.getReadableDatabase();
        Cursor cursor = mDatabase.query(getDATABASE_TABLE(uri),
                projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    //FIXME
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        if (sUriMatcher.match(uri) != OPERATION) {
            return 0;
        }
        int count = 0;
        mDatabase = mHelper.getWritableDatabase();
        count = mDatabase.update(getDATABASE_TABLE(uri),
                values, selection, selectionArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    public String getDATABASE_TABLE(Uri uri) {
        if (uri != null) {
            String path=uri.toString();
            return path.substring(path.lastIndexOf("/")+1,path.length());
        }
        return null;
    }

}
